package richbar.com.richbar;

import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.common.util.ToastUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import richbar.com.richbar.activity.login.LoginAc;
import richbar.com.richbar.base.BaseApplication;
import richbar.com.richbar.fragment.ClassificationFragment;
import richbar.com.richbar.fragment.HomePageFragment;
import richbar.com.richbar.fragment.MineFragment;
import richbar.com.richbar.fragment.ShoppingCartFragment;
import richbar.com.richbar.help.VersionHelp;

/**
 * 主页
 */
public class MainActivity extends AppCompatActivity {
    @BindView(R.id.viewpager)
    ViewPager viewpager;

    @BindView(R.id.iv_mine)
    ImageView iv_mine;
    @BindView(R.id.tv_mine)
    TextView tv_mine;

    @BindView(R.id.iv_shopcart)
    ImageView iv_shopcart;
    @BindView(R.id.tv_shopcart)
    TextView tv_shopcart;

    @BindView(R.id.iv_kind)
    ImageView iv_kind;
    @BindView(R.id.tv_kind)
    TextView tv_kind;

    @BindView(R.id.iv_home)
    ImageView iv_home;
    @BindView(R.id.tv_home)
    TextView tv_home;
    private long firstPressedTime;
    CustomLazyFragmentPagerAdapter customLazyFragmentPagerAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        initview();
    }

    private void initview() {
        setBottomLabState(0);
        customLazyFragmentPagerAdapter=new CustomLazyFragmentPagerAdapter(getSupportFragmentManager());
        viewpager.setAdapter(customLazyFragmentPagerAdapter);
        new VersionHelp(this).update(false);

    }

    /**
     * 底部类别点击事件处理
     *
     * @param view
     */
    @OnClick({R.id.ly_home, R.id.ly_mine, R.id.ly_kind, R.id.ly_shopcart})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ly_home:
                setBottomLabState(0);
                viewpager.setCurrentItem(0, false);
                break;
            case R.id.ly_kind:
                setBottomLabState(1);
                viewpager.setCurrentItem(1, false);
                break;
            case R.id.ly_shopcart:
                if(BaseApplication.uid.equals(""))
                {
                    LoginAc.start(MainActivity.this);
                }
                else
                {
                    setBottomLabState(2);
                    viewpager.setCurrentItem(2, false);

                }

                break;
            case R.id.ly_mine://我的
                if(BaseApplication.uid.equals(""))
                {
                    LoginAc.start(MainActivity.this);

                }
                else
               {
                setBottomLabState(3);
                viewpager.setCurrentItem(3, false);
               }

                break;

        }
    }

    /**
     * 点击的底部分类图标状态
     *
     * @param position
     */
    private void setBottomLabState(int position) {
        switch (position) {
            case 0:
                setTextAndImg(tv_home, iv_home, R.mipmap.icon_home_pre, true);
                setTextAndImg(tv_kind, iv_kind, R.mipmap.icon_kind, false);
                setTextAndImg(tv_shopcart, iv_shopcart, R.mipmap.icon_shopcart, false);
                setTextAndImg(tv_mine, iv_mine, R.mipmap.icon_mine, false);
                break;
            case 1:
                setTextAndImg(tv_home, iv_home, R.mipmap.icon_home, false);
                setTextAndImg(tv_kind, iv_kind, R.mipmap.icon_kind_pre, true);
                setTextAndImg(tv_shopcart, iv_shopcart, R.mipmap.icon_shopcart, false);
                setTextAndImg(tv_mine, iv_mine, R.mipmap.icon_mine, false);

                break;
            case 2:
                setTextAndImg(tv_home, iv_home, R.mipmap.icon_home, false);
                setTextAndImg(tv_kind, iv_kind, R.mipmap.icon_kind, false);
                setTextAndImg(tv_shopcart, iv_shopcart, R.mipmap.icon_shopcart_pre, true);
                setTextAndImg(tv_mine, iv_mine, R.mipmap.icon_mine, false);
                break;
            case 3:
                setTextAndImg(tv_home, iv_home, R.mipmap.icon_home, false);
                setTextAndImg(tv_kind, iv_kind, R.mipmap.icon_kind, false);
                setTextAndImg(tv_shopcart, iv_shopcart, R.mipmap.icon_shopcart, false);
                setTextAndImg(tv_mine, iv_mine, R.mipmap.icon_mine_pre, true);
                break;

        }
    }

    /**
     * 设置选择分类的样式
     *
     * @param tv
     * @param iv
     * @param res
     * @param isBlue
     */
    private void setTextAndImg(TextView tv, ImageView iv, int res, boolean isBlue) {
        iv.setImageResource(res);
        if (isBlue) {
            tv.setTextColor(ContextCompat.getColor(this, R.color.selectcolor));
        } else {
            tv.setTextColor(ContextCompat.getColor(this, R.color.gray6));
        }
    }

    private static class CustomLazyFragmentPagerAdapter extends FragmentPagerAdapter {

        private CustomLazyFragmentPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            if (position == 0) {
                return new HomePageFragment();
            } else if (position == 1) {
                return new ClassificationFragment();
            } else if (position == 2) {
                return new ShoppingCartFragment();
            } else if (position == 3) {
                return new MineFragment();
            }
            return null;
        }

        @Override
        public int getCount() {
            return 4;
        }

    }

    public interface Fragment2Fragment {
        public void gotoFragment(ViewPager viewPager);
    }

    private Fragment2Fragment fragment2Fragment;

    public void setFragment2Fragment(Fragment2Fragment fragment2Fragment, int i) {
        this.fragment2Fragment = fragment2Fragment;
        if (i == 1) {
            setTextAndImg(tv_home, iv_home, R.mipmap.icon_home, false);
            setTextAndImg(tv_kind, iv_kind, R.mipmap.icon_kind_pre, true);
            setTextAndImg(tv_shopcart, iv_shopcart, R.mipmap.icon_shopcart, false);
            setTextAndImg(tv_mine, iv_mine, R.mipmap.icon_mine, false);
        }
    }

    public void forSkip() {
        if (fragment2Fragment != null) {
            fragment2Fragment.gotoFragment(viewpager);
        }
    }

    @Override
    public void onBackPressed() {
        if (System.currentTimeMillis() - firstPressedTime < 2000){
            super.onBackPressed();
        }else {
            firstPressedTime = System.currentTimeMillis();
            ToastUtil.showToast(MainActivity.this,"再按一次退出");
        }
    }
}
